package com.gloaba.logservice.service.log;

import com.gloaba.common.core.utils.JsonUtils;
import com.gloaba.common.log.constant.LogMqConstants;
import com.gloaba.logservice.entity.SysOperLog;
import com.rabbitmq.client.Channel;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.amqp.support.AmqpHeaders;
import org.springframework.messaging.handler.annotation.Headers;
import org.springframework.messaging.handler.annotation.Payload;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

@Service
@Slf4j
@RequiredArgsConstructor
public class SysOperLogListener {

    private final ISysOperLogService iSysOperLogService;

    /**
     * 队列消息接收者
     */
    @RabbitListener(queues = LogMqConstants.LOG_OPERATE_QUEUE)
    public void messageReceiver(
            @Payload String sysOperLogBO,
            @Headers Map<String, Object> headers,
            Channel channel
    ) {
        try {
            SysOperLog sysOperLog = JsonUtils.parseObject(sysOperLogBO, SysOperLog.class);
            iSysOperLogService.saveEntity(sysOperLog);
        } catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage(), e);
        }
    }

}
